package com.ita.iosa.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ita.iosa.dao.MerchantStatusDao;
import com.ita.iosa.po.MerchantStatus;
import com.ita.iosa.util.DbUtil;

public class MerchantStatusDaoImpl implements MerchantStatusDao {

	@Override
	public int updateMerchantStatus(int mid, int status) {
		String sql = "update maudit set status=? where mid=?";

		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();

		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setInt(1, status);
			pst.setInt(2, mid);
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;

	}

	@Override
	public HashMap<Integer, Integer> selectAllMerchantStatus() {
		String sql = "select mid,status from maudit";
		HashMap<Integer, Integer> status = new HashMap<Integer, Integer>();

		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();

		try {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				status.put(rs.getInt("mid"), rs.getInt("status"));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, rs);
		}
		return status;
	}

	@Override
	public int addMerchantStatus(MerchantStatus ms) {
		String sql = "insert into maudit(mid,status,aid) values (?,?,my_seq2.nextval)";

		Connection con = null;
		PreparedStatement pst = null;
		con = DbUtil.connect();

		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			con.setAutoCommit(false);
			pst.setInt(1, ms.getMid());
			pst.setInt(2, ms.getStatus());
			m = pst.executeUpdate();
			con.commit();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, null);
		}
		return m;
	}

	@Override
	public int seleteMerchantStatusMid(int mid) {
		String sql = "select mid from maudit where mid=?";
		
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
		int midResult = -1;
		
		try {
			pst = con.prepareStatement(sql);
			pst.setInt(1, mid);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				midResult = rs.getInt("mid");
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, rs);
		}
		return midResult;
	}

	@Override
	public List<Integer> selectWaitMid() {
		String sql = "select mid from maudit where status=0";
		List<Integer> waitMids = new ArrayList<Integer>();
		
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
	
		try {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				waitMids.add(rs.getInt("mid"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, rs);
		}
		
		return waitMids;
	}

	@Override
	public List<Integer> selectPassMid() {
		String sql = "select mid from maudit where status=1";
		List<Integer> passMids = new ArrayList<Integer>();
		
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
	
		try {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				passMids.add(rs.getInt("mid"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, rs);
		}
		
		return passMids;
	}
	
	@Override
	public List<Integer> selectBlackMid() {
		String sql = "select mid from maudit where status=2";
		List<Integer> blackMids = new ArrayList<Integer>();
		
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		con = DbUtil.connect();
	
		try {
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				blackMids.add(rs.getInt("mid"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbUtil.free(con, pst, rs);
		}
		
		return blackMids;
	}
}
